package com.sdyeda.readiot.fx;

import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sdyeda.readiot.ReadIotApplication;
import com.sdyeda.readiot.api.CallApi;
import com.sdyeda.readiot.data.SetSave;
import com.sdyeda.readiot.data.SetSaveService;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;

@Component
public class SetSaveDetailController {

	private Stage stage;
	private SetSave setSave;
	

	@FXML
	private Button btnSave, btnClose,btnOpenjar;

	@FXML
	private TextField txtIottype, txtTypename,txtJarpath;

	@FXML
	private TextArea txtWritedata, txtReaddata;
	
	@FXML
	private ChoiceBox<String> cbClassname;
	
	@Autowired
	private SetSaveService service;

	@FXML
	public void initialize() {

		
		
		btnSave.setOnAction(event->{
			SaveData();
			stage.close();
		});
		
		btnClose.setOnAction(event->{
			stage.close();		
		});
		
		btnOpenjar.setOnAction(event->{
			FileChooser fc = new FileChooser();
			fc.getExtensionFilters().add(new ExtensionFilter("包文件", "*.jar"));

			File file = fc.showOpenDialog(ReadIotApplication.primaryStage);
			txtJarpath.setText(file.toURI().getPath());
			ObservableList<String> classnames = FXCollections.observableList(CallApi.FindClass(txtJarpath.getText()));

			cbClassname.setItems(classnames );
			if(classnames.size()>0) {
				cbClassname.setValue(classnames.get(0));
			}else {
				txtJarpath.setText("");
				new Alert(AlertType.ERROR,"无效的接口文件，请重新选择！").show();
			}
		});
		
		txtJarpath.setOnInputMethodTextChanged(event->{
			CallApi.FindClass(txtJarpath.getText());
		});

	}

	private void SaveData() {
		if (setSave == null) {
			//System.out.println(false);
			setSave = new SetSave();
		}
		setSave.setIottype(txtIottype.getText());
		setSave.setTypename(txtTypename.getText());
		setSave.setWritedata(txtWritedata.getText());
		setSave.setReaddata(txtReaddata.getText());
		setSave.setJarname(txtJarpath.getText());
		setSave.setClassname(cbClassname.getValue());
	
		service.save(setSave);
	}

	public void LoadData() {

		if (setSave == null) {
			System.out.println(false);
			setSave = new SetSave();
		}
		txtIottype.setText(setSave.getIottype());
		txtTypename.setText(setSave.getTypename());
		txtWritedata.setText(setSave.getWritedata());
		txtReaddata.setText(setSave.getReaddata());
		txtJarpath.setText(setSave.getJarname());
		if(setSave.getJarname()!=null && !setSave.getJarname().trim().equals("")){
			ObservableList<String> classnames = FXCollections.observableList(CallApi.FindClass(txtJarpath.getText()));
			cbClassname.setItems(classnames );
		}
		cbClassname.setValue(setSave.getClassname());
	}

	public Stage getStage() {
		return stage;
	}

	public void setStage(Stage stage) {
		this.stage = stage;
	}

	public SetSave getSetSave() {
		return setSave;
	}

	public void setSetSave(SetSave setSave) {
		this.setSave = setSave;
	}

}
